#ifndef _DEBUG_H_
#define _DEBUG_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int debug_level;
int debug_istty;


#define p_err(fmt, arg...) \
    do { \
        printf("[ERR!] %s +%d : " fmt "\n", __FILE__, __LINE__, ## arg); \
        exit(1); \
    } while(0)

#define p_warn(fmt, arg...) \
    do { \
        printf("[warn] %s +%d : " fmt "\n", __FILE__, __LINE__, ## arg); \
    } while(0)

#define p_info(fmt, arg...) \
    do { \
        printf("[info] %s +%d : " fmt "\n", __FILE__, __LINE__, ## arg); \
    } while(0)


#define spf(str, fmt, arg...) \
    do { \
        sprintf((str) + strlen(str), fmt, ## arg); \
    } while(0)

#define spf2(str, i, fmt, arg...) \
    do { \
        i += sprintf((str) + i, fmt, ## arg); \
    } while(0)


#define p_buf(buf, len) \
    do { \
        for (int i = 0; i < len; i++) \
        printf("%02X ", buf[i]); \
    } while(0)

#define p_bufx(buf, len) \
    do {                                    \
        int w = 8, f = 0;                   \
        for (int i = 0; i < len; i++) {     \
            if (i < w || i > (len - w)) {   \
                printf("%02X ", buf[i]);    \
            } else if (f == 0) {            \
                f = 1;                      \
                printf("... ");             \
            }                               \
        }                                   \
    } while(0)


#define CLEAR(x)    (memset(&(x), 0, sizeof(x)))
#define CHECK()


#endif
